/*
 * @Author: daidai
 * @Date: 2022-01-12 14:05:56
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-08-07 17:12:07
 * @FilePath: \web-pc\src\pages\big-screen\main.js
 */
import '@/qiankun/public-path'
import Vue from "vue";
import App from "./App.vue";
import VueRouter from 'vue-router'
import routes from './router'
import store from './store'
import {loading,borderBox13,digitalFlop,capsuleChart,borderBox8} from '@jiaminghi/data-view'
import { Radio,Button,RadioGroup } from 'element-ui'
import Echart from './components/echart/index.vue'
import ItemWrap from './components/item-wrap/item-wrap.vue'
import Message from './components/message/message.vue'
import Reacquire from './components/reacquire/reacquire.vue'
import Messages from './components/message/message'
import "vue-easytable/libs/theme-default/index.css";
import  '@/assets/css/public.scss'
import "@/assets/css/index.scss"

import MicroActions from '@/qiankun/qiankun-actions'




import * as filters from '@/directives/filters'

// require('./mock/mock')//是否使用mock
Vue.config.productionTip = false;

// 自定义组件
Vue.component("Echart",Echart)
Vue.component("ItemWrap",ItemWrap)
Vue.component("Message",Message)
Vue.component("Reacquire",Reacquire)
Vue.prototype.$Message =  Messages
// element组件
Vue.use(Radio);
Vue.use(Button);
Vue.use(RadioGroup)
Vue.use(VueRouter);
// datav组件
Vue.use(loading)
Vue.use(borderBox13)
Vue.use(borderBox8)
Vue.use(digitalFlop)
Vue.use(capsuleChart)
// 新增：用于保存vue实例
let instance = null
let router = null
let microPath = ''

 
if (window.__POWERED_BY_QIANKUN__) {
  microPath = '/micro-screen'
}

// 全局数据过滤器
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));
// new Vue({
//   router,
//   store,
//   render: h => h(App)
// }).$mount("#app");

/** * 新增： * 渲染函数 * 两种情况：主应用生命周期钩子中运行 / 微应用单独启动时运行 */
function render (props) {
 
  console.log('子应用render的参数', props)

  // 新增判断，如果是独立运行不执行onGlobalStateChange
  if (window.__POWERED_BY_QIANKUN__) {
    if (props) {
      // 注入 actions 实例
      MicroActions.setActions(props)
    }

    // 挂载主应用传入路由实例 用于子应用跳转主应用
    Vue.prototype.$microRouter = props.router
    Vue.prototype.$microStore = props.store
    Vue.prototype.$microEventBus = props.qiankunEventBus
    console.log('dddd', props.store)
    Vue.prototype.$maicoReLogin = props.maicoReLogin

    props.onGlobalStateChange((state, prevState) => {
      // state: 变更后的状态; prev 变更前的状态
      console.log('通信状态发生改变：', state, prevState)
      store.commit('app/setToken', state.globalToken)
    }, true)
  }

   // router不再是同一个实例，而是每次mount的时候都会新获取一个实例
   router = new VueRouter({
    routes
  })
  // 路由守卫
  router.beforeEach((to, from, next) => {
    if (to.path !== (microPath + '/login')) {
 
      if (store.state.app.token) {
        // console.log('已经登录 token=', store.state.app.token)
        if (window.__POWERED_BY_QIANKUN__ && !to.path.includes('/micro-screen')) {
          console.log('window.__POWERED_BY_QIANKUN__', window.__POWERED_BY_QIANKUN__)
          next(microPath + to.path)
        } else {
          console.log('window.__POWERED_BY_QIANKUN__11111', window.__POWERED_BY_QIANKUN__, to.path)
          next()
        }
      } else {
        console.log('子应用 - 未登录 请登录')
        next(microPath + '/login')
      }
    } else {
      next()
    }
  })
  // 挂载应用
  instance = new Vue({
    router,
    store,
    created () {
      this.$nextTick(() => {
        const dom = document.querySelector('.app-main')
        if (window.__POWERED_BY_QIANKUN__) {
             dom.style.height = 'calc(100vh - 48px)'
        } else {
          dom.style.height = 'calc(100vh)'
          dom.style.width = 'calc(100vw)'
        }
      })
    },
    render: (h) => h(App)
  }).$mount('#micro-app')
}

/**
 * 新增：
 * bootstrap 只会在微应用初始化的时候调用一次，
 下次微应用重新进入时会直接调用 mount 钩子，不会再重复触发 bootstrap。
 * 通常我们可以在这里做一些全局变量的初始化，比如不会在 unmount 阶段被销毁的应用级别的缓存等。
 */
export async function bootstrap () {
  console.log('VueMicroApp bootstraped')
}

/**
 * 新增：
 * 应用每次进入都会调用 mount 方法，通常我们在这里触发应用的渲染方法
 */
export async function mount (props) {
  console.log('VueMicroApp mount', props)

  render(props)
}
/**
 * 新增：
 * 应用每次 切出/卸载 会调用的方法，通常在这里我们会卸载微应用的应用实例
 */
export async function unmount () {
  console.log('VueMicroApp unmount')
  instance.$destroy()
  instance = null
}

// 新增：独立运行时，直接挂载应用
if (!window.__POWERED_BY_QIANKUN__) {
  console.log('直接运行')
  render()
}